package com.allen.study.infrastructure.persistence;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.allen.study.application.api.request.EmployeeLoginQueryRequest;
import com.allen.study.application.api.response.EmployeeLoginQueryResponse;
import com.allen.study.application.repository.IEmployeeLoginReadModelRepo;
import com.allen.study.domain.entity.EmployeeLogin;
import com.allen.study.domain.repository.IEmployeeLoginRepo;
import com.allen.study.common.base.Pagination;
import com.allen.study.infrastructure.persistence.assembler.EmployeeLoginPOAssembler;
import com.allen.study.infrastructure.persistence.dao.IEmployeeLoginMapper;
import com.allen.study.infrastructure.persistence.po.EmployeeLoginPO;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户登录信息表
 *
 * @author AllenSun
 * @since 2025-03-15 13:57
 */
@Service
@AllArgsConstructor
public class EmployeeLoginRepoImpl extends ServiceImpl<IEmployeeLoginMapper, EmployeeLoginPO>
        implements IEmployeeLoginRepo, IEmployeeLoginReadModelRepo {

    /**
     * 用户登录信息表持久化对象结构映射
     */
    private final EmployeeLoginPOAssembler employeeLoginPOAssembler;

    @Override
    public List<EmployeeLoginQueryResponse> query(Pagination pagination, EmployeeLoginQueryRequest queryRequest) {
        // 构造查询条件
        LambdaQueryWrapper<EmployeeLoginPO> lambdaQueryWrapper = Wrappers.lambdaQuery();


        // 如果 员工id 不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getEmployeeId())) {
            lambdaQueryWrapper.eq(EmployeeLoginPO::getEmployeeId, queryRequest.getEmployeeId());
        }
        // 如果 员工姓名 不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getEmployeeName())) {
            lambdaQueryWrapper.eq(EmployeeLoginPO::getEmployeeName, queryRequest.getEmployeeName());
        }
        // 如果 登录密码 不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getEncryptedPassword())) {
            lambdaQueryWrapper.eq(EmployeeLoginPO::getEncryptedPassword, queryRequest.getEncryptedPassword());
        }

        // 默认按照主键倒序排序
        lambdaQueryWrapper.orderByDesc(EmployeeLoginPO::getId);

        // 分页查询
        Page<EmployeeLoginPO> page = super.page(Page.of(pagination.getPageNumber(), pagination.getPageSize()), lambdaQueryWrapper);
        pagination.setTotal(page.getTotal());
        // 用户登录信息表持久化对象 转 用户登录信息表查询响应对象
        return employeeLoginPOAssembler.toQueryResponse(page.getRecords());
    }

    @Override
    public EmployeeLogin queryById(String employeeLoginId) {
        // 根据主键查询用户登录信息表
        EmployeeLoginPO employeeLoginPO = super.getById(employeeLoginId);
        // 用户登录信息表持久化对象 转 用户登录信息表响应对象
        return employeeLoginPOAssembler.toEntity(employeeLoginPO);
    }

    @Override
    public void create(EmployeeLogin employeeLogin) {
        // 用户登录信息表实体转用户登录信息表持久化对象
        EmployeeLoginPO employeeLoginPO = employeeLoginPOAssembler.assembler(employeeLogin);

        // 创建用户登录信息表
        super.save(employeeLoginPO);
    }

    @Override
    public void deleteById(String employeeLoginId) {
        // 物理删除用户登录信息表
        super.removeById(employeeLoginId);
    }

    @Override
    public void updateById(EmployeeLogin employeeLogin) {
        // 用户登录信息表实体转用户登录信息表持久化对象
        EmployeeLoginPO employeeLoginPO = employeeLoginPOAssembler.assembler(employeeLogin);
        // 更新用户登录信息表
        super.updateById(employeeLoginPO);
    }
}
